
<script src="<?php echo URL::base(true)?>media/js/jquery-1.10.1.min.js"></script>
<link rel="stylesheet" href="<?php echo URL::base(true)?>media/js/jquery-ui.css">
<script src="<?php echo URL::base(true)?>media/js/jquery-ui.js"></script>
<script src="<?php echo URL::base(true)?>media/js/highcharts.js"></script>
<script src="<?php echo URL::base(true)?>media/js/exporting.js"></script>

<style type="text/css">
td {text-align:center;height: 30px; width:180px;}
#yj3 {font-size:16px;position:relative;left:10px;
}
#yj3 td{width:180px;}
#yj4 {font-size:16px;position:relative;left:10px;
}
#yj4 td{width:180px;}
#xinxi {height:100px;}
#data {font-size:16px;position:relative;}
#data td{width:180px;height:40px;}
#yj  {font-size:15px;}
#yj td{padding:5px;}
#yj1 {font-size:15px; left:10px;}

#yj2 {font-size:15px;}
#yj2 td{padding:5px;}
#jingji {color:red;}
#xucai {color:#EE7600;}
#putong {color:blue;}
#sup {background:yellow;font-size:20px;}
.table1 thead tr td,.table1 tbody tr td{ text-align:center;}
</style>
<script type="text/javascript">
$(document).ready(function(){
  $('.table1 > tbody >tr:odd').css("background-color","#f5f5f5");
  $.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });
  //$(".datepick").datepicker();

function set_provider(data)
{
    $("#price").val(data.price);
    $("#delivery").val(data.delivery);
}

  $("#supplier_sel").change(function() {
        var providerid = $(this).val();
        var sku = $("#xinxi").text();
       var details = {"supplier_id":providerid,"sku":sku};

      $.ajax({    
      type:'post',
      url:'<?php echo url::base(true)?>warning/getproviderproducts',
      dataType:'json',
      data:{"purchase":details},
      async:false,
      success:function(data)
      {
        if(data)
        {
          // alert(data.price);
          set_provider(data);
        }
      },
      error:function()
      {
        alert('error');
      }
    });
  });

  $("#delivery").change(function(event) {
    
    var delivery = $(this).val().split("-");
    var newdate = new Date(delivery);
    var today = new Date();
    var delivery_new = (newdate.getTime() - today.getTime())/(1000*60*60*24);
    delivery_new =  Math.ceil(delivery_new);

    if(delivery_new < 0 )
    {
      alert('交货日期不能在今天之前');
      var today = new Date();
      today = today.getFullYear() +"-"+ (today.getMonth()+1) + "-"+ today.getDate();
      $(this).val(today);
    }
  });
  
  $("#target_time").change(function() {
      var target_time = parseInt($(this).val());
      var daysales = parseFloat($("#daysales").val());

      $("#quantity").val(parseInt(target_time*daysales));
  });

//advancewarning
  $("#addpurchase").click(function() {
      var sku = $("#xinxi").text();
      var url = "<?php echo url::base(true)?>warning/index?sku="+sku;
      window.open(url);
  });

 $('#searchsku').click(function(){
	var sku_value = $.trim($('#sku_value').val());
	var stamp = 0;
	$.ajax({
		type:'post',
		url:'<?php echo url::base(true);?>inventory/deliverycheck/checksku',
		dataType:'json',
		data:{param: sku_value},
		async:false,
		success:function(data)
		{
			//data=JSON.parse(data);
	    	if(data.status=='y'){
	    	}else{
	    		stamp = 1;
		    	alert('未找到 '+data.sku+' 的对应信息');return;
	    	}
		},
		error:function(){
			alert("error");
		}
	});
	//alert(stamp);return;
	if(stamp==1)return;
	$("#searchform").submit();
 });
 $('#container_sale').highcharts({
            title: {
                text: '历史销售记录',
                x: -20 //center
            },
            subtitle: {
                text: 'Source: ERP',
                x: -20
            },
            xAxis: {
                categories: [<?php if(isset($sale['date'])){
                foreach ($sale['date'] as $key => $value) {
                           echo "'".$value."',";
                 
                }}?>],
                labels:{
                rotation: 30,//旋转
                  enabled: true, 
                  step: 10
                }
            },

            yAxis: {
                title: {
                    text: '计量单位'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: '/计量单位'
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [{
                name: '<?php echo $sku_warning['sku']?>销量',
                data: [<?php if(isset($sale['sale'])){
                foreach ($sale['sale'] as $key => $value) {
                  echo $value.',';
                }};?>]
            }]
        });

 $('#container_inventory').highcharts({
            title: {
                text: '历史库存记录/历史采购记录',
                x: -20 //center
            },
            subtitle: {
                text: 'Source: ERP',
                x: -20
            },
            xAxis: 
                {
                    categories: [<?php if(isset($purchase['date'])){
                    foreach ($purchase['date'] as $key => $value) {
                               echo "'".$value."',";
                     
                    }}?>],
                    labels:{
                      rotation: 30,
                      enabled: true, 
                      step: 10,
                      //y:0
                   }
            },

            yAxis: {
                title: {
                    text: '计量单位'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: '/计量单位'
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [
                {
                name: '<?php echo $sku_warning['sku']?>库存',
                data: [<?php if(isset($inventory['inventory'])){
                foreach ($inventory['inventory'] as $key => $value) {
                  echo $value.',';}};?>]
                },
                {
                name: '<?php echo $sku_warning['sku']?>采购',
                data: [<?php if(isset($purchase['purchase'])){
                foreach ($purchase['purchase'] as $key => $value) {
                  	echo $value.',';}};?>]
                },
            ]
        });
});


</script>




<div style="font-size:22px;position:relative;left:10px;margin-top:30px;">查询条件:</div>

<div style="background-color:red">

</div>

 
<div style="margin-top:20px;margin-left:20px;">
 
<form action="<?php echo url::base(true)?>advancewarning/index" method="get" id="searchform">
SKU:<input style="margin-left:4px;" type="text" name="sku" id="sku_value">
<input type="button" value="查询" class="btn btn-info" id="searchsku" style="margin-left:5px;">
</form>
</div>

<!-- 产品基本信息-->
<?php if(isset($sku_detailed['sku'])){?>
<div style="font-size:22px;position:relative;left:10px;margin-top:30px;">产品基本信息:</div>
<div id="yj3">
  <table border = "1px" style="margin-top:10px">
    <tr style="background-color:rgba(129,208,177,.3);">
      <td>图片</td>
      <td>SKU</td>
      <td>中文名称</td>
      <td>规格</td>
      <td>储位</td>
       <td>成本</td>
    </tr>
    <tr>
    <?php if(isset($sku_detailed)){?>
      <td rowspan ="4" style="width:120px;height:200px;word-break:break-all;overflow:hidden;">
     <img style="width:150px;height:180px" src="<?php echo $sku_detailed['filename'];?>"/>
     <br> <a style="font-size:14px" href='' target="_blank"></a></td>
      <td id="xinxi"><?php echo $sku_detailed['sku'];?></td>
      <td><?php echo  $sku_detailed['products_name_cn'];?></td>
      <td><?php echo $sku_detailed['size'];?></td>
      <td><?php echo $sku_detailed['products_place'];?></td>
       <td><?php echo $sku_detailed['products_values'];?></td>
      <?php }?>
    </tr>
    <tr height="10px" style="background-color:rgba(129,208,177,.3)">
    
      <td>供应商价格</td>
      
      <td>供应商</td>
      <td>上新时间</td>
      <td>日均销量</td>
      <td></td>
    </tr>
    <tr>
      <td >  
      <span id="providers_price">
      <?php
   		echo isset($sku_providers[0])?$sku_providers[0]['price']:'---';
      ?>
      </span>
      </td>
      <td>
      <?php echo isset($sku_providers[0])?$sku_providers[0]['vProviderName']:'---'
      ?>
     
      </td>
      <td></td>
      <td>
        <input type="hidden" id="daysales" value="<?php echo $sku_warning['daysales']?>">
        <?php echo round($sku_warning['daysales'],2)?>
      </td>
      <td></td>
    </tr>
  </table>
</div>
<?php }?>

<!-- 产品详细日均销量-->
<div style="font-size:22px;position:relative;left:10px;margin-top:30px;">产品详细日均销量:</div>
<div id="yj4">
  <table border = "1px" style="margin-top:10px">
    <tr style="background-color:rgba(129,208,177,.3)">
      <td>SKU</td>
      <td>中文名称</td>
      <td>7天日均销量</td>
      <td>14天日均销量</td>
      <td>21天日均销量</td>
      <td>28天日均销量 </td>
      <td>加权日均销量 </td>
    </tr>
    <tr>
      <td><?php echo $sku_detailed['sku'];?></td>
      <td><?php echo  $sku_detailed['products_name_cn'];?></td>
      <td><?php echo round($sku_warning['daysales7']/7,2)?></td>
      <td><?php echo round($sku_warning['daysales14']/14,2)?></td>
      <td><?php echo round($sku_warning['daysales21']/21,2)?></td>
      <td><?php echo round($sku_warning['daysales28']/28,2)?></td>
      <td><?php echo round($sku_warning['daysales'],2)?></td>
    </tr>

  </table>
</div>

<div style="font-size:22px;position:relative;left:10px;margin-top:30px;">还能支持天数:</div>
<div id="yj4">
  <table border = "1px" style="margin-top:10px">
    <tr style="background-color:rgba(129,208,177,.3)">
      <td>SKU</td>
      <td>中文名称</td>
      <td>实际库存</td>
      <td>总在途</td>
      <td>实际可用库存</td>
      <td>加权日均销量</td>
      <td>安全库存</td>
      <!-- <td>安全库存校正值</td> -->
			<!-- <td>权重</td> -->
      <td>还能支持几天</td>
      <td>在途天数</td>
      <td>预警提示</td>
      <td>最佳采购日期</td>
    </tr>
    <tr>
      <td><?php echo $sku_detailed['sku'];?></td>
      <td><?php echo  $sku_detailed['products_name_cn'];?></td>
      <td><?php echo $sku_warning['inventory']['actual_num']?></td>
      <td><?php echo $sku_warning['inventory']['pp_num']?></td>
      <td><?php echo $sku_warning['inventory']['actual_available_num']?></td>
      <td><?php echo round($sku_warning['daysales'],2)?></td>
      <td><?php echo round($sku_warning['daysales']*$sku_warning['wave_days'],2)?></td>
      <!-- <td></td> -->
      <!-- <td></td> -->
      <td><?php echo round($sku_warning['supp_days'],2);?></td>
      <td><?php echo round($sku_warning['delivery'],0);?></td>
    		
    	<td> <?php echo service_warning::get_warning_tips($sku_warning); ?></td>    		
      <td><?php echo (round($sku_warning['best'],0)<90)?date('Y-m-d',strtotime(round($sku_warning['best'],0).'days')):''?></td>
    </tr>

  </table>
</div>


<div style="font-size:22px;position:relative;left:10px;margin-top:30px;">建议采购数据:</div>
<br/>
<table border = "1px" style=" margin-left:10px;">
<tr style="background-color:rgba(129,208,177,.3);">
<td>SKU</td>
<td>中文名称</td>
<td>供应商</td>
<td>供应商价格</td>
<td>供应商交货日期</td>
<td>采购时间</td>
<td>目标时间段</td>
<td>采购数量</td>
<!-- <td>操作</td> -->
</tr>
<tr>
<td><?php echo $sku_detailed['sku'];?></td>
<td><?php echo  $sku_detailed['products_name_cn'];?></td>

<td><!-- 供应商 -->
<?php if($sku_providers){?>
<select class="input-medium" name="supplier_id[]" 
                              id="supplier_sel">供应商
                              <?php 
                              foreach ($sku_providers as $provider){?>
                              <!--修改索引 iproviderID->id -->
                              <option value="<?php echo $provider['id']?>">
                              <!--修改索引 vProviderName->name -->
                            <?php echo $provider['name']?>
                              </option> 
                              <?php }?>
                              </select>
                              <?php }?>
                            </td>
<td>
  <!-- 价格 -->
   <?php if(isset($sku_providers[0]['price'])){?>
<input type="text" name="single_price" class="input-mini" 
value="<?php if(isset($sku_providers[0]['price'])){ echo $sku_providers[0]['price'] ;}?>"
             readonly="readonly"                 id="price"/>
<?php }?>
</td>
<td>
  <?php if(isset($sku_providers[0]['delivery'])){?>
  <input type="text" name="delivery" readonly="readonly" class="input-medium datepick"
  value="<?php if(isset($sku_providers[0]['price'])){
    echo ($sku_providers[0]['delivery']<90)?date('Y-m-d',strtotime($sku_providers[0]['delivery'].'days')):'';}
    else {
      echo date('Y-m-d');
    }?>"
  id="delivery"/>
  <?php }?>

</td>
<td>
 <?php echo (round($sku_warning['best'],0)<90)?date('Y-m-d',strtotime(round($sku_warning['best'],0).'days')):''?>

</td>
<td>
  <input type="text" style="width:40px;margin:0 auto;" id="target_time"
value="<?php echo $sku_warning['target_time']?>" readonly="readonly"/>&nbsp;/天
</td>
<td>
<input type="text"  
value="<?php echo ceil($sku_warning['target_time']*$sku_warning['daysales']); ?>" readonly="readonly" 
  id="quantity"
  style="width:40px;margin:0 auto;" class="amount" />
</td>

<!-- 
 <td>
  <?php //if(isset($sku_providers) and count($sku_providers) > 0){?>
  <input type="button" value="确定采购" style="width:80px;margin:0 auto;" id="addpurchase"></td>-->
 <?php // }?>
</tr>
</table>
</div>
 

<div style="font-size:22px;margin-top:40px; margin-left:10px;">库存详情:</div>
<div id="yj1" style="margin-top:20px;font-size:16px;position:relative;">
  <table  border = "1px">
    <tr style="background-color:rgba(129,208,177,.3);">
      <td>SKU</td>
      <td>实际库存</td>
      <td>实际可用库存</td>
      <td>在途库存</td>
      <td>占用量</td>
      <td>总可用</td>
    </tr>
    <tr>
      <td><?php echo $sku_warning['sku']?></td>
      <td><?php echo $sku_warning['inventory']['actual_num']?></td>
      <td><?php echo $sku_warning['inventory']['actual_available_num']?></td>
      <td><?php echo $sku_warning['inventory']['pp_num']?></td>
      <td><?php echo $sku_warning['inventory']['op_print_num']?></td>
      <td><?php echo $sku_warning['inventory']['all_available_num']?></td>
    </tr>
  </table>
</div>

<!-- <div id="container_warehouse"></div> -->
<div style="font-size:22px;position:relative;left:10px;margin-top:30px;">历史库存记录:</div>
<div id="container_inventory"></div>


<div style="font-size:22px;position:relative;left:10px;margin-top:30px;">历史销售记录</div>
<div id="container_sale"></div>



<?php if(isset($purchase_details) and !empty($purchase_details)){?>
<div style="font-size:22px;margin-top:40px;">采购明细:</div>
<div id="yj" style="margin-top:20px;">
  <table border = "1px" id="xiangxi" class="table table1">
  <thead>
    <tr style=" background-color:#2f96b4;color:#fff;">
      <td>采购单号</td>
      <td>SKU</td>
      <td>中文名称</td>
      <td>采购单价</td>
      <td>采购量</td>
      <td>供应商</td>
      <td>采购时间</td>
      <td>采购人员ID</td>
      <td>在途时间</td>
      <td>状态</td>
    </tr></thead><tbody>
    <?php  foreach ($purchase_details as $purchase_detail){?>
        <tr>
      <td><?php echo $purchase_detail['purchaseorder_id']?></td>
      <td><?php echo $purchase_detail['sku']?></td>
      <td><?php echo $purchase_detail['sku_name']?></td>
      <td><?php echo $purchase_detail['single_price']?></td>
      <td><?php echo $purchase_detail['quantity']?></td>
      <td><?php

//        -----------------------------------------kdw--------------------------------------
//        $provider_name = Service_Warning::get_provider_by_id($purchase_detail['supplier']);
          $provider_name = Service_Warning::get_provider_by_id($purchase_detail['purchaseorder_id']);
//        -----------------------------------------kdw--------------------------------------

      if($provider_name)
      {
      	echo $provider_name['vProviderName'];	
  	  }?></td>
      <td><?php echo date('Y-m-d',strtotime($purchase_detail['purchaseorder_date']))?></td>
      <td><?php echo $purchase_detail['purchase_id']?></td>
      <td><?php if($on_way_time=Service_Warning::get_onway_time($purchase_detail['purchaseorder_id'],$purchase_detail['sku']))
      {echo round($on_way_time,1);}?></td>
      <td><?php $details_status = Service_warning::get_purchase_details_status($purchase_detail['details_status']);
      if($details_status)
      {
      	echo $details_status['value'];
  	   }?></td>
    </tr>
    <?php } ?>
    </tbody>
  </table>
</div>
<?php }?>


